﻿using System.Windows.Forms.DataVisualization.Charting;
using TomatoClock.Models;

namespace TomatoClock.Views
{
    public partial class StatisticsControl : UserControl
    {
        private Data currentData;

        public StatisticsControl()
        {
            InitializeComponent();
            InitializeChart();
        }

        public StatisticsControl(Data data)
        {
            this.currentData = data;
            InitializeComponent();
            InitializeChart();
        }

        public void SetData(Data data)
        {
            this.currentData = data;
            if (data != null)
            {
                tomatoNumLabel.Text = data.TomatoNum.ToString();
                tomatoNumSumLabel.Text = data.TomatoNumSum.ToString();
                tomatoNumWeeklyLabel.Text = data.TomatoNumWeekly.ToString();
                focusTimeLabel.Text = data.FocusTime.ToString();
                focusTimeWeeklyLabel.Text = data.FocusTimeWeekly.ToString();
                focusTimeSumLabel.Text = data.FocusTimeSum.ToString();
                taskNumLabel.Text = data.TaskNum.ToString();
                taskNumWeeklyLabel.Text = data.TaskNumWeekly.ToString();
                taskNumSumLabel.Text = data.TaskNumSum.ToString();
                interruptNumLabel.Text = data.InterruptNum.ToString();
                interruptNumWeeklyLabel.Text = data.InterruptNumWeekly.ToString();
                interruptNumSumLabel.Text = data.InterruptNumSum.ToString();
            }
        }

        private void InitializeChart()
        {
            // 创建并配置 Chart 控件

            ChartArea chartArea = new ChartArea("ChartArea1");
            WeekChart.ChartAreas.Add(chartArea);

            Series series = new Series("FocusTime")
            {
                ChartType = SeriesChartType.Column,
                XValueType = ChartValueType.String,
                YValueType = ChartValueType.Double
            };
            WeekChart.Series.Add(series);

            WeekChart.Titles.Add("周专注时长");
            chartArea.AxisX.Title = "Day of the Week";
            chartArea.AxisY.Title = "专注时长(分钟)";
            chartArea.AxisX.Interval = 1;
            chartArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.LightGray;
            chartArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.LightGray;

            series.Color = System.Drawing.Color.Blue;
            series.IsValueShownAsLabel = true;

            Controls.Add(WeekChart);
        }

        private void DisplayFocusTimeChart()
        {
            if (WeekChart.Series["FocusTime"] != null)
            {
                WeekChart.Series["FocusTime"].Points.Clear();
            }

            // 准备数据
            var focusTimeData = new Dictionary<string, double>
            {
                { "Monday", 25 },
                { "Tuesday", 30 },
                { "Wednesday", 40 },
                { "Thursday", 35 },
                { "Friday", 50 },
                { "Saturday", 10 },
                { "Sunday", 20 }
            };

            // 添加数据点到 Chart 控件
            foreach (var dataPoint in focusTimeData)
            {
                WeekChart.Series["FocusTime"].Points.AddXY(dataPoint.Key, dataPoint.Value);
            }
        }

        private void StatisticsControl_Load(object sender, EventArgs e)
        {
            DisplayFocusTimeChart();
        }
    }
}
